package com.ss.android.download;

import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.Bundle;
import android.os.Looper;
import android.os.Message;
import android.os.SystemClock;
import android.text.TextUtils;
import android.util.Log;

import com.bytedance.common.utility.collection.WeakHandler;
import com.bytedance.common.utility.collection.WeakHandler.Callback;

import java.util.HashMap;
import java.util.Iterator;

public final class l implements MediaScannerConnection.MediaScannerConnectionClient, Callback {
    static final class l$a {
        public final long a;
        public final String b;
        private String c;

        public l$a(long arg2, String arg4, String arg5) {

            this.a = arg2;
            this.b = arg4;
            this.c = arg5;
            SystemClock.elapsedRealtime();
        }

        public final void a(MediaScannerConnection arg3) {
            arg3.scanFile(this.b, this.c);
        }
    }

    final MediaScannerConnection a;
    HashMap b;
    private final Context c;
    private WeakHandler d;

    public l(Context arg3) {

        this.d = new WeakHandler(Looper.getMainLooper(), ((Callback) this));
        this.b = new HashMap();
        this.c = arg3;
        this.a = new MediaScannerConnection(arg3, ((MediaScannerConnection.MediaScannerConnectionClient) this));
    }

    public final void handleMsg(Message message) {
        String v5 = null;
        if (message != null) {
            Bundle v0 = message.getData();
            if (v0 != null) {
                String v1 = v0.getString("path");
                String v0_1 = v0.getString("uri");
                if (!TextUtils.isEmpty(((CharSequence) v1)) && !TextUtils.isEmpty(((CharSequence) v0_1))) {
                    Uri v2 = Uri.parse(v0_1);
                    MediaScannerConnection v3 = this.a;
                    Object v0_3;
                    synchronized (v3) {
                        v0_3 = this.b.remove(v1);
                    }
                    if (v0_3 != null) {
                        ContentValues v1_1 = new ContentValues();
                        v1_1.put("scanned", Integer.valueOf(1));
                        if (v2 != null) {
                            v1_1.put("mediaprovider_uri", v2.toString());
                        }

                        if (k.a(this.c).a(ContentUris.withAppendedId(com.ss.android.download.a.a.a, ((l$a) v0_3).a), v1_1, v5, ((String[]) null)) != 0) {
                            return;
                        }

                        if (this.c == null) {
                            return;
                        }

                        this.c.getContentResolver().delete(v2, v5, null);
                    } else {
                        Log.w("SsDownloadManager", "Missing request for path " + v1);
                        return;
                    }
                }
            }
        }
    }

    public final void onMediaScannerConnected() {
        MediaScannerConnection v1 = this.a;
        synchronized (v1) {
            Iterator v2 = this.b.values().iterator();
            while (v2.hasNext()) {
                ((com.ss.android.download.l.l$a) v2.next()).a(this.a);
            }
            return;
        }
    }

    public final void onScanCompleted(String arg5, Uri arg6) {
        Message v0 = this.d.obtainMessage();
        Bundle v1 = new Bundle();
        v1.putString("path", arg5);
        v1.putString("uri", arg6.toString());
        v0.setData(v1);
        this.d.sendMessage(v0);
    }
}

